package com.mindainfo.groundwave.server.device.service;

import com.mindainfo.groundwave.common.api.device.service.IDeviceOperateService;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.ResultFactory;
import com.mindainfo.groundwave.server.device.bean.DeviceOperate;
import com.mindainfo.groundwave.server.device.dao.IDeviceOperateDao;
import com.mindainfo.groundwave.server.device.mapper.DeviceOperateMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by MD-01 on 2017/6/13.
 */
@Service
@Transactional
public class DeviceOperateServiceImpl implements IDeviceOperateService {

    private Logger logger = LoggerFactory.getLogger("设备操作");

    @Resource
    private IDeviceOperateDao deviceOperateDao;
    @Resource
    private DeviceOperateMapper deviceOperateMapper;


    @Override
    public CommonResult<Void> open(Integer doorId) {

        logger.info("请求开门操作");

        DeviceOperate operate = new DeviceOperate();
        operate.setDoorId(doorId);
        operate.setOperate("OpenDoor");
        operate.setState(0);
        operate.setTime(new Date());
        //int id = deviceOperateDao.add(operate);
        deviceOperateMapper.insert(operate);
        int id = operate.getId();
        if(id>0){
            logger.info("开门请求发送成功,id="+id);
            return ResultFactory.ok();
        }else {
            return ResultFactory.error("数据插入错误");
        }
    }
}
